package com.jic.market.impl;

import com.jic.market.common.constant.PatchGrouponLimitConstant;
import com.jic.market.common.constant.PatchGrouponThemeConstant;
import com.jic.market.common.exception.MarketErrorCodeEnum;
import com.jic.market.common.exception.MarketException;
import com.jic.market.entity.PatchGrouponLimitEntity;
import com.jic.market.mapper.PatchGrouponLimitEntityMapper;
import com.jic.market.request.PatchGrouponThemeRequest;
import com.jic.market.service.PatchGrouponLimitService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author guohuayong
 * @date: 2020年02月26日
 */
@Service
@Slf4j
public class PatchGrouponLimitServiceImpl implements PatchGrouponLimitService {
    @Resource
    private PatchGrouponLimitEntityMapper curMapper;

    @Override
    public Integer addLimitByUser(Long id, PatchGrouponThemeRequest request) {
        if (PatchGrouponThemeConstant.LimitType.LIMIT.compare(request.getIndividualJoinType())) {
            return null;
        }

        if (request.getIndividualJoinTime() < 1) {
            log.info("单用户参团限制-个人参团限制次数 --- 设置错误={}", request.getIndividualJoinTime());
            throw new MarketException(MarketErrorCodeEnum.PARAM_ERROR);
        }

        PatchGrouponLimitEntity param = PatchGrouponLimitEntity.builder().
                refPatchGrouponTheme(id)
                .limitType(PatchGrouponLimitConstant.LimitType.USER.getCode())
                .limitRef(id)
                .totalLimit(request.getIndividualJoinTime())
                .finishedTimes(0)
                .customerId(1L)
                .build();

        int result = curMapper.insert(param);
        if (result < 1) {
            throw new MarketException(MarketErrorCodeEnum.SQL_ERROR);
        }

        return result;
    }
}
